package com.yxplearn.mybatisplus.collision;

import lombok.Data;

import java.awt.*;

@Data
public class BallS {

    //随机小球半径
    public double r;
    public Color color;
    //随机小球的位置 图形左上角
    public double x;
    public double y;
    //小球的圆心
    public double f;
    public double m;
    //小球速度X轴1和-1;
    public double speedX;
    public double speedY;


    public static double randomNum(double m, double n) {
        return Math.floor(Math.random() * (n - m + 1) + m);
    }

    public BallS(int width, int height) {
        //随机小球半径
        this.r = randomNum(15, 30);
        this.color = new Color((int) randomNum(0, 255), (int) randomNum(0, 255), (int) randomNum(0, 255));
        //随机小球的位置
        this.x = randomNum(this.r, width - this.r);
        this.y = randomNum(this.r, height - this.r);

        this.f = x + r;
        this.m = y + r;

        //小球速度X轴1和-1;
        this.speedX = randomNum(1, 0) > randomNum(0, 1) ? 1 : -1;
        this.speedY = randomNum(1, 0) > randomNum(0, 1) ? 1 : -1;
    }

//    //小球移动
//    public void move(double width, double height) {
//        this.setX(this.getX() + this.getSpeedX());
//        this.setY(this.getY() + this.getSpeedY());
//        //判断是否碰到边界
//        //左边界
//        if (this.getX() <= 2 * this.getR()) {
//            this.setX(this.getR());
//            //反弹
//            this.setSpeedX(this.getSpeedX() * -1);
//        }
//        //右边界
//        if (this.getX() >= width - 2 * this.getR()) {
//            this.setX(width - 2 * this.getR());
//            this.setSpeedX(this.getSpeedX() * -1);
//        }
//        // 上边界
//        if (this.getY() <= this.getR()) {
//            this.setY(0.5 * this.getR());
//            this.setSpeedY(this.getSpeedY() * -1);
//        }
//        // 下边界
//        if (this.getY() >= height - 0.5 * this.getR()) {
//            this.setY(height - 0.5 * this.getR());
//            this.setSpeedY(this.getSpeedY() * -1);
//        }
//    }

    public void move(double width, double height, MyRectangle myRectangle) {
        this.f += this.speedX;
        this.m += this.speedY;

        //判断是否碰到边界
        //左边界
        if (this.f <= this.r) {
            this.f = this.r;
            //反弹
            this.speedX *= -1;
        }
        //右边界
        if (this.f >= width - this.r) {
            this.f = width - this.r;
            this.speedX *= -1;
        }
        // 上边界
        if (this.m <= this.r) {
            this.m = this.r;
            this.speedY *= -1;
        }
        // 下边界
        if (this.m >= height - this.r) {
            this.m = height - this.r;
            this.speedY *= -1;
        }

        // 边的碰撞
        // 矩形左侧反射
        if (this.f + this.r >= myRectangle.x && f < myRectangle.x && this.m >= myRectangle.y && this.m <= myRectangle.y + myRectangle.height) {
            this.f = myRectangle.x - this.r;
            this.speedX *= -1;
        }

        // 矩形上届反射
        if (this.m + this.r >= myRectangle.y && m < myRectangle.y && this.f >= myRectangle.x && this.f <= myRectangle.x + myRectangle.width) {
            this.m = myRectangle.y - this.r;
            this.speedY *= -1;
        }

        // 矩形右侧
        if (this.f <= myRectangle.x + myRectangle.width + this.r && f > myRectangle.x + myRectangle.width && this.m >= myRectangle.y && this.m <= myRectangle.y + myRectangle.height) {
            this.f = myRectangle.x + myRectangle.width + this.r;
            this.speedX *= -1;
        }
        // 下边
        if (this.m <= myRectangle.y + myRectangle.height + this.r && m > myRectangle.y + myRectangle.height && this.f >= myRectangle.x && this.f <= myRectangle.x + myRectangle.width) {
            this.m = myRectangle.y + myRectangle.height + this.r;
            this.speedY *= -1;
        }

        // 顶点碰撞 左上角
        if (this.f <= myRectangle.x && f >= myRectangle.x - r && this.m <= myRectangle.y && m >= myRectangle.y - r) {
            double distance = Math.sqrt(Math.pow(f - myRectangle.x, 2) + Math.pow(m - myRectangle.y, 2));
            if (distance <= r) {
                if (x >= y) {
                    speedY *= -1;
                } else {
                    speedX *= -1;
                }
            }
        }

        // 左下
        if (this.f <= myRectangle.x && f >= myRectangle.x - r && this.m <= myRectangle.y + myRectangle.height + r && m >= myRectangle.y + myRectangle.height) {
            double distance = Math.sqrt(Math.pow(f - myRectangle.x, 2) + Math.pow(m - (myRectangle.y + myRectangle.height), 2));
            if (distance <= r) {
                if (x >= y) {
                    speedX *= -1;
                } else {
                    speedY *= -1;
                }
            }
        }

        // 右上角
        if (this.f <= myRectangle.x + myRectangle.width + r && f >= myRectangle.x + myRectangle.width && this.m <= myRectangle.y + myRectangle.height && m >= myRectangle.y - r) {
            double distance = Math.sqrt(Math.pow(f - (myRectangle.x + myRectangle.width), 2) + Math.pow(m - myRectangle.y, 2));
            if (distance <= r) {
                if (x >= y) {
                    speedX *= -1;
                } else {
                    speedY *= -1;
                }
            }
        }

        // 右下角
        if (this.f <= myRectangle.x + myRectangle.width + r && f >= myRectangle.x + myRectangle.width && this.m <= myRectangle.y + myRectangle.height + r && m >= myRectangle.y + myRectangle.height) {
            double distance = Math.sqrt(Math.pow(f - (myRectangle.x + myRectangle.width), 2) + Math.pow(m - (myRectangle.y + myRectangle.height), 2));
            if (distance <= r) {
                if (x >= y) {
                    speedX *= -1;
                } else {
                    speedY *= -1;
                }
            }
        }

        this.x = f - r;
        this.y = m - r;

    }

}
